package com.pkcargo.util.spider.httpclient;

import com.googlecode.util.date.DateCommon;
import com.googlecode.util.HashQueueSet;
import com.pkcargo.entity.cargo.Corp;
import com.pkcargo.entity.search.Metadata;
import com.pkcargo.entity.user.User;
import com.pkcargo.service.cargo.CorpManager;
import com.pkcargo.util.spider.WebClient;
import com.pkcargo.util.WebProperties;
import net.spy.memcached.MemcachedClient;
import org.apache.commons.lang.StringUtils;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class XiaMenHaiYun extends WebClient {
    @Autowired
    private CorpManager corpManager;
    private User user = new User(2L);//根据该网站的用户ID来确定
    private String initUrl = "http://www.xmhaiyun.com/InfoManage/InfoDetails.aspx?InfoID=";
    private String xmhyUrl;

    public String getXmhyUrl() {
        return xmhyUrl;
    }

    public void setXmhyUrl(String xmhyUrl) {
        this.xmhyUrl = xmhyUrl;
    }

    public static Set<Integer> runtime_url_set_cache;
    @Override
    public void spiderJob() throws Exception {
        String webContent = getWebContent(xmhyUrl);
        Set set = filterAllUrl(webContent);
        if (set != null && set.size() != 0) {
            Iterator iterator = set.iterator();//取出元素
            logger.info("【厦门海运网】任务启动...");
            while (iterator.hasNext()) {
                webContent = getWebContent((String) iterator.next());
                if (webContent.indexOf("Server Error in '/' Application.") < 0) {
                    buildMetadata(webContent, (String) iterator.next());
                }
            }
        } else
            logger.info("【厦门海运网】任务启动...无更新数据");
    }

    //和默认缓存的Set集合中的Url进行比较，如果没有则添加并返回
        @SuppressWarnings(value="unchecked")
    private Set filterAllUrl(String webContent)  throws IOException  {
        MemcachedClient c = new MemcachedClient(new InetSocketAddress("192.168.3.10", 11211));
        runtime_url_set_cache = (Set<Integer>)c.get("url_int");
        if(runtime_url_set_cache == null){
              runtime_url_set_cache = new HashQueueSet<Integer>(13);
        }
        Set<String> set = new HashSet<String>();
        //先获取文本中主要的链接部分
        pre = "<font color=\"#F7F7F7\"><b>类型</b></font></th><th scope=\"col\"><font color=\"#F7F7F7\"><b>标题</b></font></th><th scope=\"col\"><font color=\"#F7F7F7\"><b>发布日期</b></font></th>";
        post = "<!-- AspNetPager V6.0.0 for VS2005  Copyright:2003-2006 Webdiyer (www.webdiyer.com) -->";
        webContent = StringUtils.substringBetween(webContent, pre, post);
        //再获取其中的ID
        pre = "<a href=\"InfoDetails.aspx?InfoID=";
        post = "\" target=\"_blank\">";
        String[] ids = StringUtils.substringsBetween(webContent, pre, post);
        for (String id : ids) {
            if (runtime_url_set_cache.add(Integer.parseInt(id))) {
                set.add(initUrl + id);
            }
        }
        c.set("url_int", 7200, runtime_url_set_cache) ; //保存到memcached，保持2小时
        return set;
    }

    public void testInit() throws Exception {
        for (int id = 25; id < 30; id++) {
            String webContent = getWebContent(initUrl + id);
            System.out.println(initUrl + id);
            if (webContent.indexOf("Server Error in '/' Application.") < 0) {
                buildMetadata(webContent, initUrl + id);
            }
        }
    }

    private void buildMetadata(String webContent, String url) {
        Metadata metadata = new Metadata();
        Corp corp = new Corp();
        //content
        pre = "<textarea name=\"_ctl0:_ctl0:ContentPlaceHolder1:ContentPlaceHolder1:TextBoxContent\" id=\"_ctl0__ctl0_ContentPlaceHolder1_ContentPlaceHolder1_TextBoxContent\">";
        post = "</textarea>";
        corp.setContent(StringUtils.substringBetween(webContent, pre, post));
        //contact
        pre = "<input name=\"_ctl0:_ctl0:ContentPlaceHolder1:ContentPlaceHolder1:TextBoxTele\" type=\"text\" value=\"";
        post = "\" id=\"_ctl0__ctl0_ContentPlaceHolder1_ContentPlaceHolder1_TextBoxTele\"";
        corp.setContact(StringUtils.substringBetween(webContent, pre, post));
        //title
        pre = "<input name=\"_ctl0:_ctl0:ContentPlaceHolder1:ContentPlaceHolder1:TextBoxTitle\" type=\"text\" value=\"";
        post = "\" id=\"_ctl0__ctl0_ContentPlaceHolder1_ContentPlaceHolder1_TextBoxTitle\" />";
        metadata.setTitle(StringUtils.substringBetween(webContent, pre, post));
        metadata.setUser(user);
        metadata.setUrl(url);
        //发布时间网页上不准确，所以用抓取时间代替
        metadata.setPublishTime(new Date());
        metadata.setCaptureTime(new Date());
        metadata.setValidTime(DateCommon.strToDate("2999-11-11"));
        metadata.setType(WebProperties.TYPE_CORP);
        metadata.setAllContent(corp.getContent());
        metadata.setValid(true);
        try {
            corp.setMetadata(metadata);
            corpManager.save(corp);
            logger.info("【厦门海运网】抓取成功。标题：" + metadata.getTitle());
            System.out.println("保存厦门海运信息成功：" + metadata.getTitle());
        }
        catch (ConstraintViolationException ex) {
            logger.error("【厦门海运网】保存地址已经存在");
        }
        catch (Exception e) {
            System.out.println("保存失败");
            logger.error("【厦门海运网】保存失败" + e);
        }
    }
}
